﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
using System.Net.Sockets;

namespace Bacon_FTP_Client
{
    /// <summary>
    /// Directory Treeview creator class.
    /// </summary>
    class MyTreeViews
    {
        /// <summary>
        /// Constructor Method.
        /// </summary>
        public MyTreeViews()
        {
            
        }

        /// <summary>
        /// Directory Treeview creation method.
        /// This method accepts a Treeview instance and returns a Bool confirmation.
        /// </summary>
        /// <param name="myTreeView">Treeview Instance.</param>
        /// <returns>Confirmation.</returns>
        public bool createTree(TreeView myTreeView)
        {
            bool returnValue = false;

            try
            {
                //create desktop node
                TreeNode desktopNode = new TreeNode();
                desktopNode.Text = "Desktop";
                desktopNode.Tag = "Desktop";
                desktopNode.Nodes.Add("");
                myTreeView.Nodes.Add(desktopNode);

                //get drive information nodes
                foreach (DriveInfo drv in DriveInfo.GetDrives())
                {
                    TreeNode fileChild = new TreeNode();

                    if (drv.DriveType == DriveType.CDRom)
                    {
                        fileChild.ImageIndex = 1;
                        fileChild.SelectedImageIndex = 1;
                    }
                    else if (drv.DriveType == DriveType.Fixed)
                    {
                        fileChild.ImageIndex = 0;
                        fileChild.SelectedImageIndex = 0;
                    }
                    fileChild.Text = drv.Name;
                    fileChild.Nodes.Add("");
                    myTreeView.Nodes.Add(fileChild);
                    returnValue = true;
                }

            }
            catch (Exception)
            {
                returnValue = false;
                //throw;
            }
            return returnValue;
        }

        /// <summary>
        /// Enumerate Directory Method.
        /// This method enumerates the nodes on the directory Treeview method.
        /// </summary>
        /// <param name="parentNode">Node.</param>
        /// <returns>Node.</returns>
        public TreeNode EnumerateDirectory(TreeNode parentNode)
        {
            try
            {
                DirectoryInfo rootDir;

                //Fill the desktop node
                Char [] arr = {'\\'};
                string[] nameslist = parentNode.FullPath.Split(arr);
                string path = "";

                if (nameslist.GetValue(0).ToString() == "Desktop")
                {
                    path = SpecialDirectories.Desktop + "\\";

                    for (int i = 1; i < nameslist.Length; i++)
                    {
                        path += nameslist[i] + "\\";
                    }

                    rootDir = new DirectoryInfo(path);
                }

                //Fill other nodes
                else
                {
                    rootDir = new DirectoryInfo(parentNode.FullPath + "\\");
                }

                parentNode.Nodes[0].Remove();

                foreach (DirectoryInfo drv in rootDir.GetDirectories())
                {
                    TreeNode node = new TreeNode();
                    node.Text = drv.Name;
                    node.Nodes.Add("");
                    parentNode.Nodes.Add(node);
                }

                //Fill the files
                foreach (FileInfo file in rootDir.GetFiles())
                {
                    TreeNode node = new TreeNode();
                    node.Text = file.Name;
                    node.ImageIndex = 2;
                    node.SelectedImageIndex = 2;
                    parentNode.Nodes.Add(node);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw 
            }

            return parentNode;
        }

    }
}
